<?php

namespace app\api\controller;

use app\admin\model\Coupon as CouponModel;
use app\admin\model\Usercoupon as Usercoupon;
use app\common\controller\Api;
use think\Config;
use think\Env;
use think\Log;
use think\Validate;

/**
 * 优惠券接口
 */
class Coupon extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    public function _initialize(){
        parent::_initialize();

        $this->CouponModel = new CouponModel;
        $this->Usercoupon = new Usercoupon;
    }
    /**
     * 优惠券列表
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $coupon = $this->CouponModel->where('end_time','>',time());
        $coupon = $this->coupon($coupon,$user);
        $coupon = $coupon->select();
        $this->success('请求成功',[
            'coupon'=>$coupon,
        ]);
    }
    public function coupon($coupon,$user)
    {
        $collect = db('user_coupon')->where('user_id',$user->id)->field('id,coupon_id,user_id')->select();
        $arr = [];
        if(!empty($collect)){
            foreach ($collect as $v){
                array_push($arr,$v['coupon_id']);
            }
        }
        $coupon = $coupon->whereNotIn('id',$arr);
        return $coupon;
    }
    /**
     * 我的优惠券
     * @return void
     */
    public function mycoupon()
    {
        $user = $this->auth->getUser();
        $this->Usercoupon
            ->where('status',0)
            ->where('end_time','<=',time())
            ->update([
                'status' => 2
            ]);
        $coupon = $this->Usercoupon->where('user_id',$user->id);
        $type = request()->param('status',0);
        $coupon = $coupon->where('status',$type)->select();
        $this->success('我的优惠券',$coupon);
    }

    /**
     * 领取优惠券
     * @return void
     */
    public function add()
    {
        $user = $this->auth->getUser();
        $data = request()->param();
        if(!$data['coupon_id']){
            $this->error('参数不全');
        }
        foreach ($data['coupon_id'] as $v){
            $coupon = $this->CouponModel->where('id',$v)->find();
            if(!$coupon){
                $this->error('优惠券已下架');
            }
            $log = $this->Usercoupon->where(['user_id'=>$user->id,'coupon_id'=>$v])->find();
            if(!$log){
//                $this->success('成功');
                $this->Usercoupon->insert([
                    'user_id'=>$user->id,
                    'coupon_id'=>$v,
                    'title'=>$coupon['title'],
                    'start_time'=>$coupon['start_time'],
                    'end_time'=>$coupon['end_time'],
                    'coupon_price'=>$coupon['coupon_price'],
                    'use_min_price'=>$coupon['use_min_price'],
                    'status'=>0,
                    'createtime'=>time(),
                    'updatetime'=>time()
                ]);
            }
        }
        $this->success('成功');
    }

    /**
     * 使用优惠券
     * @return void
     */
    public function usecoupon()
    {
        $coupon_id = request()->param('coupon_id');
        if(!$coupon_id){
            $this->error('参数错误');
        }
        $coupon = $this->Usercoupon->where('id',$coupon_id)->find();
        if(!$coupon || $coupon['status'] != 0){
            $this->error('优惠券不存在或已使用');
        }
        $this->Usercoupon->where('id',$coupon_id)->update([
            'status' => 3
        ]);
        $this->success('成功');
    }

    /**
     * 取消使用
     * @return void
     */
    public function cancel()
    {
        $data = request()->param();
        if(!$data['coupon_id']){
            $this->error('参数错误');
        }
        $this->Usercoupon->whereIn('id',$data['coupon_id'])->update([
            'status' => 0
        ]);
        $this->success('成功');
    }
}
